package com.hao.dead;

public class DeadLock {
    public static void main(String[] args) {
        Makeup name1 = new Makeup(0, "灰姑娘");
        Makeup name2 = new Makeup(1, "白雪公主");
        name1.start();
        name2.start();
    }
}
class Lipstick{
}
class Mirror {
}
class Makeup extends Thread {
    // 用户static保证资源只有一个
    static Lipstick lipstick = new Lipstick();
    static Mirror mirror = new Mirror();
    int choice;
    String name;

    public Makeup(int choice, String name) {
        this.choice = choice;
        this.name = name;
    }
    @Override
    public void run() {
        // 化妆
        try {
            makeup();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    private void makeup() throws InterruptedException {
        if (choice==0){
            synchronized (lipstick){//获取口红的锁
                System.out.println(this.name+"获取口红的锁");
                Thread.sleep(1000);
            }
            synchronized (mirror){ // 1s后 获取镜子
                System.out.println(this.name+"获取镜子的锁");
            }
        }else {
            synchronized (mirror){
                System.out.println(this.name+"获取镜子的锁");
                Thread.sleep(1000);
            }
            synchronized (lipstick){
                System.out.println(this.name+"获取口红的锁");
            }
        }
    }
}
